% V20210224 - 14.5 JS [2] INCLUDE "GW.bas" timing = 10 % seconds % Create a page. p = GW_NEW_PAGE() % Prepare title bar string. Title$ = GW_ADD_BAR_TITLE$("Complex JS Example") % Add title to page. GW_ADD_TITLEBAR(p, Title$) % Add a progressbar to the page. progressbar = GW_ADD_PROGRESSBAR(p, "Ready to compile.") % Script to auto-update progress-bar % Usage: 1. call JS("var pgini=20") % 2. call JS("autoIncreasePgBar(+50, 3000)") % increase progress bar of 50 % in 3 seconds pg$ = GW_ID$(progressbar) e$ ="var pgcur=0; var interval;function doPg(tgt)" e$+="{pgcur+=0.1;$('#"+pg$+"').val(parseFloat(pgini)" e$+="+parseFloat(pgcur));$('input[name="+pg$+"]').slider" e$+="('refresh');if(pgcur>=tgt){clearInterval(interval);}}" e$+="function autoIncreasePgBar(tgtDelta, tgtTime){" e$+="interval=setInterval(function(){doPg(tgtDelta);}," e$+="0.1*tgtTime/tgtDelta);}" GW_INJECT_HTML(p, "") % Add some space and a button to start operations. GW_ADD_TEXT(p, "") start_btn = GW_ADD_BUTTON(p, "Start compilation!", "START") % Show the page. GW_RENDER(p) DO % Wait for user action. r$ = GW_WAIT_ACTION$() % User clicked on "Start compilation" IF r$ = "START" % Disable the button GW_DISABLE(start_btn) c0 = CLOCK() % record the current time % Auto-increment the progressbar from 0 to 100% in timing*1000 milliseconds JS("var pgini=0") JS("autoIncreasePgBar(+100, "+INT$(1000*timing)+")") % Simulate dummy operations, but you could % run real resource-consuming tasks instead! GW_MODIFY(progressbar, "text", "Copying files needed for compilation...") PAUSE 1500 e$ = "Changing package name in files... " FOR i = 1 TO 142 GW_MODIFY(progressbar, "text", e$ + "("+INT$(i)+"/142)") PAUSE 25 ignore$ = GW_ACTION$() % read and ignore user actions NEXT FOR i = 1 TO 5 GW_MODIFY(progressbar, "text", "Adding resource #"+INT$(i)) PAUSE 250 ignore$ = GW_ACTION$() % read and ignore user actions NEXT GW_MODIFY(progressbar, "text", "Compiling...") cf = c0 + timing*1000 % time when the script finishes cw = cf - CLOCK() % time to wait until script finishes IF cw > 1 THEN PAUSE cw + 1000 % wait until then + some buffer GW_MODIFY(progressbar, "text", "Done! Press the back key to exit") ignore$ = GW_ACTION$() % read and ignore user actions until now ENDIF % Now wait that the BACK key is pressed. UNTIL r$ = "BACK" END "End of JS Complex example."